home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 331_01 / ged7.c < prev    next >
Text File  |  1990-06-12  |  8KB  |  338 lines

  1. /*
  2. HEADER:         CUG199.07;
  3. TITLE:          GED (nee QED) screen editor -- part 7;
  4. DATE:           12/17/86;
  5.  
  6. DESCRIPTION:   "Low level terminal i/o functions for the GED editor.
  7.                 Putch, getch, etc.";
  8. KEYWORDS:       putch, getch;
  9. SYSTEM:         MS-DOS;
  10. FILENAME:       GED7.C;
  11. AUTHORS:        G. Nigel Gilbert, James W. Haefner, Mel Tearle, G. Osborn;
  12. COMPILERS:      DeSmet C;
  13. */
  14.  
  15. /*   e/qed/ged  screen editor
  16.  
  17.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  18.            August-December 1981
  19.  
  20.     Modified:  Aug-Dec   1984:   BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  21.                March     1985:   BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  22.                May       1986:   converted to ged - Mel Tearle
  23.  
  24.     FILE:      ged7.c
  25.  
  26.     FUNCTIONS: getkey, testkey, getlow, testlow, getscankey,
  27.                inkey, putret, dispch, putch, chkbuf, gettime,
  28.                inword, uspr, beep, dohelp
  29.  
  30.     PURPOSE:   low level terminal i/o functions and additions
  31.  
  32. */
  33.  
  34. #include <ctype.h>  /* for Microsoft only */
  35. #include "ged.h"
  36.  
  37. char timestr[9],prevstr[9];
  38.  
  39. /* wait for a key to be pressed & return it,
  40.  * translating to internal codes
  41.  */
  42. unsigned char getkey()
  43. {
  44.     unsigned char testkey();
  45.     unsigned char c;
  46.  
  47.     while ( !(c = testkey()) )
  48.         ;
  49.     return c;
  50. }
  51.  
  52.  
  53. /* if a key has been pressed, return it, else 0
  54.  * see pcio.a for mods to scr_ci
  55.  */
  56. unsigned char testkey()
  57. {
  58.     unsigned char inkey();
  59.     unsigned char c, *tranp;
  60.     int  i;
  61.  
  62.     if ( c = inkey() ) {
  63.  
  64.         if ( c == *(tranp = tran) )  {
  65.             while ( !( c = inkey()) )
  66.                 ;
  67.             c |= PARBIT;
  68.         }
  69.         for ( i = 1, tranp++; i < NKEYS; i++, tranp++ )  {
  70.             if ( c == *tranp )
  71.                 return i;
  72.         }
  73.         if (c >= ' ')
  74.             return c;
  75.         else
  76.             return 0;
  77.     }
  78.     else {
  79.         show_time(0);
  80.         return 0;
  81.     }
  82. }
  83.  
  84.  
  85. /* get a key, converting control and u/c keys to l/c,
  86.  * translation of ESCKEY only
  87.  */
  88. unsigned char getlow()
  89. {
  90.     unsigned char c;
  91.     unsigned char testlow();
  92.  
  93.     while ( !(c = testlow()) );
  94.     return  c;
  95. }
  96.  
  97.  
  98. /* test for a key, convert upper
  99.  * and control chars to lower case
  100.  */
  101. unsigned char testlow()
  102. {
  103.     unsigned char inkey();
  104.     unsigned char c;
  105.  
  106.     if ( !( c = inkey() ) )              return  0;
  107.     if ( tran[ESCKEY] == c )             return  ESCKEY;
  108.     if ( c >= F1KEY  &&  c  <= F10KEY )  return  c;
  109.  
  110.     if ( c < ' ') c = c + 96;
  111.  
  112.     return ( c >= 'A' && c <= 'Z' ? c + 32 : c );
  113. }
  114.  
  115.  
  116. /* get a key, translation of ESCKEY, CR, LEFTKEY,
  117.  * DELLEFT, RETRIEVE only
  118.  */
  119. unsigned char getscankey()
  120. {
  121.     unsigned char inkey();
  122.     unsigned char c;
  123.  
  124.     while ( !( c = inkey()) );
  125.     if (tran[ESCKEY] == c)   return ESCKEY_P;
  126.     if (tran[CR] == c)       return CR_P;
  127.     if (tran[LEFTKEY] == c)  return LEFTKEY_P;
  128.     if (tran[DELLEFT] == c)  return DELLEFT_P;
  129.     if (tran[RETRIEVE] == c) return RETRIEVE_P;
  130.     return c;
  131. }
  132.  
  133.  
  134. /* Dequeues and returns the next input character.  chkbuf() can be called
  135.  * from anywhere to keep the input buffer current.  This buffer is
  136.  * redundant with the BIOS input buffer, but is used to detect excessive
  137.  * backlog.  Returns 0 if no key.  Never waits.
  138.  */
  139. unsigned char inkey()
  140. {
  141.     unsigned char c;
  142.     int i;
  143.  
  144.     if (chkbuf()) {
  145.         c = inbuf[0];
  146.         inbufp--;
  147.         for (i = 0; i < inbufp; i++)
  148.             inbuf[i] = inbuf[i+1];
  149.         return c;
  150.     }
  151.     else {
  152.         return 0;
  153.     }
  154. }
  155.  
  156. /* Returns the next input character but leaves the character in the queue.
  157.  * Allows a look-ahead capability without the commitment of processing
  158.  * the next command.  Returns 0 if no key.  Never waits.
  159.  */
  160. unsigned char peekkey()
  161. {
  162.     if (chkbuf())
  163.         return inbuf[0];
  164.     else
  165.         return 0;
  166. }
  167.  
  168. /* Dequeue and return last key pressed.
  169.  
  170.  
  171. /* type a CR/LF
  172.  */
  173. putret()
  174. {
  175.     putch('\r');
  176.     putch('\n');
  177. }
  178.  
  179.  
  180. /* type a character, in different intensity
  181.  * if non-printable turned off - mt.
  182.  */
  183. dispch(c)
  184. char c;
  185. {
  186.     if ( c < ' ' )  {
  187. /*  makeother();         */
  188. /*  _os( DIRIO, c+64 );  */
  189. /*  makeother();         */
  190.     }
  191.     else
  192.         scr_co(c);
  193. }
  194.  
  195.  
  196. /* type a character to crt test for keyboard input
  197.  */
  198. putch(c)
  199. char c;
  200. {
  201.     char cc;
  202.  
  203.     scr_co(c);
  204.     chkbuf();
  205. }
  206.  
  207.  
  208. /*
  209.  * Returns 0 if buffer is empty; otherwise returns the backlogged character
  210.  * count.  First inputs and queues character if any.
  211.  * show_time not used here only because it might slow some operations.
  212.  */
  213. int chkbuf()
  214. {
  215.     char cc;
  216.  
  217.     if ( ( cc = scr_csts() ) != 0  &&  inbufp < BUFFER )
  218.         inbuf[inbufp++] = cc;
  219.     return inbufp;
  220. }
  221.  
  222.  
  223.  
  224.  
  225. int gettime()
  226. {
  227.     unsigned  hours, mins, secs;
  228.  
  229.     rax = 0x2C00;
  230.     syscall();
  231.  
  232.     mins  = rcx  &  0x00ff;
  233.     hours = rcx  >>  8;
  234.     secs = (rdx >> 8) & 0x00ff;
  235.     sprintf( timestr, "%02d:%02d:%02d", hours, mins, secs );
  236.     return secs;
  237. }
  238.  
  239.  
  240. /* refresh time field in status line if time has changed (change==0),
  241.  * or unconditionally (change==1) */
  242. show_time(change)
  243. int change;
  244. {
  245.     int sav;  /* this variable must not be global */
  246.     gettime();
  247.  
  248.     if (blankedmess || change || strcmp( timestr, prevstr ) != 0 )  {
  249.         strcpy( prevstr, timestr );
  250.         sav = curson(NO);
  251.         scr_aputs( TIMEPOS, 0, timestr, ATTR3 );
  252.         gotoxy(cursorx-offset,cursory);
  253.         curson(sav);
  254.     }
  255.     return;
  256. }
  257.  
  258.  
  259. /* return true if c is a letter, digit or punctuation
  260.  * not very elegant but lots of control, used to delete a word
  261.  */
  262. /* DeSmet version
  263.  * inword(c)
  264.  * char c;
  265.  * {
  266.  *   return  isalnum(c)  ||  index( "\"\\!@#$%^&*()_+-[]{};'`:~,./<>?|", c );
  267.  * }
  268.  */
  269.  
  270. /* Microsoft version */
  271. inword(c)
  272. char c;
  273. {
  274.     char *strchr();
  275.  
  276. /*   return  isalnum(c) || *strchr( "\"\\!@#$%^&*()_+-[]{};'`:~,./<>?|", c );*/
  277.     return  isalnum(c) || *strchr( "\"\\!@#$%^&*()_+-[]{};'`:~./<>?|", c );
  278. }
  279.  
  280. /* print 'n' as a number, return number of chars typed
  281.  * useful in debugging
  282.  */
  283. int uspr(sn,ln,attri)
  284. unsigned int sn,attri;
  285. long int ln;
  286. {
  287.     int temp;
  288.     long n;
  289.  
  290.     n = 0;
  291.     if ( !ln )
  292.         n = sn;
  293.     else
  294.         n = ln;
  295.  
  296.     if ( n < 10 )  {
  297.         scr_putch( ( unsigned char )(n+'0'), attri );
  298.         return 1;
  299.     }
  300.     temp = uspr( 0, n/10, attri );
  301.     uspr( 0, n%10, attri );
  302.  
  303.     return  temp+1;
  304. }
  305.  
  306.  
  307. /* display help menu
  308.  */
  309. dohelp()
  310. {
  311.     int y;
  312.  
  313.     puttext();
  314.     topline = 11;
  315.     calp();
  316.     cleareop(1);
  317.     gotoxy(0,1);
  318.  
  319. /* display the help menu
  320.  */
  321.     putstr("LEFT:  char  <--, ^S     word ^A    line ^<--, ^U\n");
  322.     putstr("RGHT:  char  -->, ^D     word ^F    line ^-->, ^]\n");
  323.     putstr("UP:    window ^W    line ^E    page ^R, <PgUp>    file <Home>\n");
  324.     putstr("DOWN:  window ^Z    line ^X    page ^C, <PgDn>    file <End>\n");
  325.     putstr("DELETE: char ^G, <Del>, <BS>;  word ^T,   line  ^Y,  block ^K,  rpl ^V, <ins>\n");
  326.     putstr("undo ^-   push line ^Y   pop ^P   copypop ^O   linejump ^J\n");
  327.     putstr("Search/rpl: F3 up agn, F4 dn agn, ^L agn, ^QF,F2 new search, ^QA replace\n");
  328.     putstr("<esc> cancel, ^K  block/output/read,  ^Q  find/rpl/files/paragraph\n");
  329.     putstr("F1 help on/off  F6 cent. window.  F7 opts, F8 dir, F9 bye, F10 save\n");
  330.     putstr("-------------------------------------------------------------------------------\n");
  331.  
  332.     putpage();
  333.     return;
  334. }
  335.  
  336.  
  337.  
  338.